package cn.iocoder.yudao.module.ym.controller.admin.sitelog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.sitelog.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.sitelog.SiteLogDO;
import cn.iocoder.yudao.module.ym.service.sitelog.SiteLogService;

@Tag(name = "管理后台 - 日志")
@RestController
@RequestMapping("/ym/site-log")
@Validated
public class SiteLogController {

    @Resource
    private SiteLogService siteLogService;

    @PostMapping("/create")
    @Operation(summary = "创建日志")
    public CommonResult<Long> createSiteLog(@Valid @RequestBody SiteLogSaveReqVO createReqVO) {
        return success(siteLogService.createSiteLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日志")
    public CommonResult<Boolean> updateSiteLog(@Valid @RequestBody SiteLogSaveReqVO updateReqVO) {
        siteLogService.updateSiteLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日志")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSiteLog(@RequestParam("id") Long id) {
        siteLogService.deleteSiteLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SiteLogRespVO> getSiteLog(@RequestParam("id") Long id) {
        SiteLogDO siteLog = siteLogService.getSiteLog(id);
        return success(BeanUtils.toBean(siteLog, SiteLogRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得日志分页")
    public CommonResult<List<SiteLogRespVO>> getSiteLogList(@Valid SiteLogPageReqVO pageReqVO) {
        List<SiteLogDO> pageResult = siteLogService.getSiteLogList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SiteLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日志分页")
    public CommonResult<PageResult<SiteLogRespVO>> getSiteLogPage(@Valid SiteLogPageReqVO pageReqVO) {
        PageResult<SiteLogDO> pageResult = siteLogService.getSiteLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SiteLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日志 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSiteLogExcel(@Valid SiteLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SiteLogDO> list = siteLogService.getSiteLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日志.xls", "数据", SiteLogRespVO.class,
                        BeanUtils.toBean(list, SiteLogRespVO.class));
    }

}